<template>
  <div style="padding: 24px">
    <el-button type="primary" @click="handleAdd">新增</el-button>
    <el-button
      type="primary"
      @click="handleExport"
      v-hasPermi="['bus:active:export']"
      >导出</el-button
    >
    <h2>激活码列表</h2>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column type="index" width="50"> </el-table-column>
      <el-table-column label="激活码" prop="activeCode"> </el-table-column>
      <el-table-column width="180" label="时长">
        <template slot-scope="scope">
          <span>
            {{
              scope.row.activeDays == "99999"
                ? "永久"
                : scope.row.activeDays + "天"
            }}
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="activeUsed" width="180" label="是否使用">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.activeUsed == '0'"
            >未使用</el-tag
          >
          <el-tag type="danger" v-if="scope.row.activeUsed !== '0'"
            >已使用</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="handleDelete(scope.row)" type="text" size="small"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="float: right"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-size="limit"
      layout="total, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <el-dialog title="添加激活码" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
      <el-form :model="form">
        <el-form-item label="VIP类型" :label-width="formLabelWidth">
          <el-select
            v-model="form.dictCode"
            placeholder="请选择VIP分类"
            style="width: 100%"
          >
            <el-option
              v-for="(item, index) in dictCodeList"
              :key="index"
              :label="
                item.dictLabel == '99999' ? '永久' : item.dictLabel + '天'
              "
              :value="item.dictCode"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生成数量" :label-width="formLabelWidth">
          <el-input
            type="number"
            v-model.number="form.codeNum"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import request from "@/utils/request";
export default {
  data() {
    return {
      tableData: [],
      total: 0,
      limit: 20,
      page: 1,
      dialogFormVisible: false,
      dictCodeList: [],
      form: {
        dictCode: "",
        codeNum: "",
      },
      formLabelWidth: "120px",
    };
  },
  created() {
    this.getSaleList();
  },
  methods: {
    getSaleList() {
      request({
        url:
          "/bus/active/list?pageSize=" +
          this.limit +
          "&pageNum=" +
          this.page,
        method: "get",
      }).then((res) => {
        this.tableData = res.rows;
        this.total = res.total;
      });
    },
    handleCurrentChange(page) {
      this.page = page;
      this.getSaleList()
    },
    handleDelete(e) {
      request({
        url: "/bus/active/" + e.activeId,
        method: "delete",
      }).then((res) => {
        console.log(res);
        this.page = 1;
        this.getSaleList();
        alert("删除成功");
      });
    },
    handleAdd() {
      request({
        url: "/bus/outer/dictData/vip_price",
        method: "get",
      }).then((res) => {
        this.dictCodeList = res.data;
      });
      this.dialogFormVisible = true;
    },
    handleSubmit() {
      request({
        url: "/bus/active",
        method: "post",
        data: this.form,
      }).then((res) => {
        this.page=1
        this.getSaleList();
        alert("添加成功");
      });
      this.dialogFormVisible = false;
      console.log(this.form);
    },
    handleExport() {
      request({
        url: "/bus/active/export",
        method: "get",
      }).then((res) => {
        window.location.href =
          process.env.VUE_APP_BASE_API +
          "/common/download?fileName=" +
          encodeURI(res.msg) +
          "&delete=" +
          true;
      });
    },
  },
};
</script>